*Aggregates raw data
*Version 15 Stata

set more off

*Establish working directories
local d3 Geographic Data
local d5 Antidepressant Data
local d22 Entropy Data

*Bring in the anti-depressant data; generate list all drugs which appear
cd `d5'
forvalues date=2011/2015 {
use princeton_Antidepressant_yr`date'.dta, clear

keep provider_id product year month payment_type patient_gender patient_age sum_tot_scripts sum_new_scripts

sort provider_id product payment_type patient_gender patient_age
by provider_id product payment_type patient_gender patient_age: egen sum_tot_scripts_year=sum(sum_tot_scripts)
by provider_id product payment_type patient_gender patient_age: egen sum_new_scripts_year=sum(sum_new_scripts)
by provider_id product payment_type patient_gender patient_age: generate count=_n
keep if count==1

keep provider_id product year payment_type patient_gender patient_age sum_tot_scripts_year sum_new_scripts_year

destring year, replace
replace year = 1

rename year in_`date'
rename sum_new_scripts_year new_`date'
rename sum_tot_scripts_year tot_`date'

sort provider_id product
save provider_product_cat_`date'.dta, replace
}

*Fix the year specific variables so that append will work
cd `d5'
forvalues year=2006(1)2015 {
use provider_product_cat_`year'.dta, clear
rename new_`date' new_scripts
rename tot_`date' tot_scripts
save, replace
}

*Merge all years of anti-psychotic drug lists
cd `d5'
use provider_product_cat_2006.dta, clear
forvalues year=2007(1)2015 {
append using provider_product_cat_`year'.dta
}
generate year = 2006
forvalues date=2006(1)2015 {
replace year=`date' if in_`date'==1
}
drop in*
save provider_product_cat_allyears.dta, replace

*Delete the individual year files
cd `d5'
forvalues date=2006/2015 {
erase provider_product_cat_`date'.dta
}